package com.rockcent.common.utils;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by DK on 16/12/12.
 * excel报表导出
 */
public class ExcelExportUtils {

    /**
     *  通用导出excel报表
     * @param title 表头
     * @param columnName 字段名
     * @param exportDataList 导出的数据
     * @throws IOException
     * @throws WriteException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public static void excelExport(String[] title, String[] columnName, List<?> exportDataList, HttpServletResponse response) throws IOException, WriteException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = sdf.format(date);
//        response.setContentType("application/vnd.ms-excel");
//        response.setHeader("Content-Disposition", "attachment; filename=" + filename);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");   //这个是可用的
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((filename+".xls").getBytes("utf-8"), "ISO8859-1"));
        ServletOutputStream outputStream = response.getOutputStream();

        // 创建可以写入的Excel工作薄(默认运行生成的文件在tomcat/bin下 )
        WritableWorkbook wwb = Workbook.createWorkbook(outputStream);
        // 生成工作表,(name:First Sheet,参数0表示这是第一页)
        WritableSheet sheet = wwb.createSheet("第一页", 0);

        // 设置单元格的文字格式
        WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false,
                UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        WritableCellFormat wcf = new WritableCellFormat(wf);
        wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
        wcf.setAlignment(Alignment.CENTRE);
        sheet.setRowView(1, 500);
        CellView cellView = new CellView();
        cellView.setAutosize(true);
        sheet.setColumnView(0, cellView);

        for (int j = 0; j < title.length; j++) {
            sheet.addCell(new Label(j, 0, title[j], wcf));
        }

        // 写入内容
        for (int col = 0; col < exportDataList.size(); col++) {
            Object object = exportDataList.get(col);
            Class clazz = object.getClass();
            for (int j = 0; j < columnName.length; j++) {
                String first = columnName[j].substring(0, 1);
                first = first.replace(first, first.toUpperCase());
                columnName[j] = first + columnName[j].substring(1, columnName[j].length());
                sheet.setColumnView(col, cellView);
                Method method = clazz.getDeclaredMethod("get" + columnName[j]);
                sheet.addCell(new Label(j, col + 1, String.valueOf(method.invoke(object)) == "null" ? "" : String.valueOf(method.invoke(object))));
            }
        }
        wwb.write();
        // 关闭文件
        wwb.close();
        // 关闭输出流
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
